package com.example.myapplication3;

import androidx.appcompat.app.AppCompatActivity;

import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.HashMap;

public class List3Activity extends ListActivity implements AdapterView.OnItemClickListener,AdapterView.OnItemLongClickListener {
    private static final String TAG ="List3Activity" ;  //AppCompatActivity
    private ArrayList<HashMap<String,String>> listItems;//存放文字、图片
    Handler handler;
    private SimpleAdapter listItemAdapter;//适配器
    private int msgWhat=7;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //setContentView(R.layout.activity_list3);  AppCompatActivity
        //ListView list3=findViewById(R.id.mylist3);AppCompatActivity
        //data
        listItems = new ArrayList<HashMap<String, String>>();
        for(int i=0;i<10;i++){
            HashMap<String,String>map=new HashMap<String,String>();//必须放在里面，map是多个
            map.put("ItemTitle","Rate:"+i);//标题文字
            map.put("ItemDetail","detatil:"+i);//详情描述
            listItems.add(map);
        }
        SimpleAdapter listItemAdapter=new SimpleAdapter(this,
                listItems,//listItem数据源
                R.layout.list_item,//Listitem的Xml布局，要有两个布局，一个是map的布局，还有一个本页面的布局
                new String[]{"ItemTitle","ItemDetail"},
                new int[]{R.id.itemTitle,R.id.itemDetail}
                );

        MyAdapter adapter=new MyAdapter(this,R.layout.list_item,listItems);
        //list3.setAdapter(listItemAdapter); AppCompatActivity
        //getListView().setAdapter(listItemAdapter);与下一句同样功能
        setListAdapter(listItemAdapter);
        getListView().setOnItemClickListener(this);
        getListView().setOnItemLongClickListener(this);
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Log.i(TAG,"onItemClick:position="+position);
        Object itemAtPosition=getListView().getItemAtPosition(position);
        HashMap<String,String>map=(HashMap<String,String>) itemAtPosition;
        String titleStr=map.get("ItemTitle");
        String detailStr=map.get("ItemDetail");
        Log.i(TAG,"onItemClick:titlestr="+titleStr);
        Log.i(TAG,"onItemClick:detailstr="+detailStr);

        TextView title=(TextView) view.findViewById(R.id.itemTitle);
        TextView detail=(TextView) view.findViewById(R.id.itemDetail);
        String title2=String.valueOf(title.getText());
        String detail2=String.valueOf(detail.getText());
        Log.i(TAG,"onItemClick:title2="+title2);
        Log.i(TAG,"onItemClick:detail2="+detail2);
    }

    @Override
    public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
        Log.i(TAG,"onItemCick"+"长按事件处理");
        //长按时显示对话框
        AlertDialog.Builder builder=new AlertDialog.Builder(this);
        builder.setTitle("提示");
        builder.setMessage("请确认是否删除当前数据");
        builder.setPositiveButton("是", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Log.i(TAG,"onClick:对话框事件处理");
                //删除数据项
                //去看ratelistactivity2
            }
        });
        return false;
    }
}